package com.example.audiobooks.base;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.audiobooks.MainActivity;
import com.example.audiobooks.R;
import com.example.audiobooks.home.bean.BooksBean;
import com.example.audiobooks.user.man.UserLoginActivity;
import com.example.audiobooks.utils.CartStorage;
import com.example.audiobooks.utils.Constants;

public class BooksInfoActivity extends Activity implements View.OnClickListener{

    private ImageButton ibBookInfoBack;
    private ImageButton tvMoreShare;
    private ImageView ivBookInfoImage;
    private TextView tvBookInfoName;
    private TextView tvBookInfoDesc;
    private TextView tvBookInfoPrice;
    private LinearLayout llBooksRoot;
    private TextView tvBookInfoCallcenter;
    private TextView tvBookInfoCollection;
    private TextView tvBookInfoCart;
    private Button btnBookInfoAddcart;
    private BooksBean books_bean;

    private void findViews() {
        ibBookInfoBack = (ImageButton)findViewById( R.id.ib_book_info_back );
        tvMoreShare = (ImageButton)findViewById( R.id.tv_more_share );
        ivBookInfoImage = (ImageView)findViewById( R.id.iv_book_info_image );
        tvBookInfoName = (TextView)findViewById( R.id.tv_book_info_name );
        tvBookInfoDesc = (TextView)findViewById( R.id.tv_book_info_desc );
        tvBookInfoPrice = (TextView)findViewById( R.id.tv_book_info_price );
        llBooksRoot = (LinearLayout)findViewById( R.id.ll_books_root );
        tvBookInfoCallcenter = (TextView)findViewById( R.id.tv_book_info_callcenter );
        tvBookInfoCollection = (TextView)findViewById( R.id.tv_book_info_collection );
        tvBookInfoCart = (TextView)findViewById( R.id.tv_book_info_cart );
        btnBookInfoAddcart = (Button)findViewById( R.id.btn_book_info_addcart );

        ibBookInfoBack.setOnClickListener( this );          //返回
        tvMoreShare.setOnClickListener( this );             //分享
        btnBookInfoAddcart.setOnClickListener( this );      //加入播放列表
        tvBookInfoCallcenter.setOnClickListener( this );     //试听
        tvBookInfoCollection.setOnClickListener( this );     //喜欢
        tvBookInfoCart.setOnClickListener( this );          //播放列表
    }

    @Override
    public void onClick(View v) {
        if ( v == ibBookInfoBack ) {

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    SharedPreferences.Editor editor = getSharedPreferences("data", 0).edit();
                    editor.putInt("recent_key", 1);
                    editor.apply();
                }
            }, 1000);
            //关闭当前页面
            finish();
            Intent intent = new Intent(BooksInfoActivity.this, MainActivity.class);
            startActivity(intent);
            //登录成功，重新刷新主页
            //退出成功，重新刷新主页
        } else if ( v == tvMoreShare ) {
            Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
            // Handle clicks for tvMoreShare
            //详情页右上角分享
        } else if ( v == btnBookInfoAddcart ) {
            SharedPreferences preferences = getSharedPreferences("data", 0);
            int join_key = preferences.getInt("join_key", 0);
            if (join_key == 1) {
                CartStorage.getInstance().addData(books_bean);
                Toast.makeText(this, "加入播放列表", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "请先登录！！！", Toast.LENGTH_SHORT).show();
            }
            // Handle clicks for btnBookInfoAddcart
        }else if ( v == tvBookInfoCallcenter ) {
            Toast.makeText(this, "试听", Toast.LENGTH_SHORT).show();
            // Handle clicks for btnBookInfoAddcart
        }else if ( v == tvBookInfoCollection ) {
            Toast.makeText(this, "喜欢", Toast.LENGTH_SHORT).show();
            // Handle clicks for btnBookInfoAddcart
        }else if ( v == tvBookInfoCart ) {
            Toast.makeText(this, "播放列表", Toast.LENGTH_SHORT).show();
            // Handle clicks for btnBookInfoAddcart
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_books_info);
        findViews();
        //接收数据
        Intent intent = getIntent();
        books_bean = (BooksBean) intent.getSerializableExtra("booksBean");

        if(books_bean != null){
            Log.e("TAG", "接收到图书详细信息onCreate: "+books_bean.toString() );
//            Toast.makeText(this, "booksbean", Toast.LENGTH_SHORT).show();
            //如果接收到的数据不为空，则设置数据。
            setDataForView(books_bean);
        }
    }
    private  void setDataForView(BooksBean books_bean){
        String name = books_bean.getName();
        String cover_price = books_bean.getCover_price();
        String figure = books_bean.getFigure();
        String product_id = books_bean.getProduct_id();
        Glide.with(this).load(Constants.IMAGE_URL +
                figure).into(ivBookInfoImage);
        if (name != null) {
            //设置名称
            tvBookInfoName.setText(name);
        }
        if (cover_price != null) {
            //设置阅读量
            tvBookInfoPrice.setText("阅读量：" + cover_price);
        }
    }
}